package com.madev.isellcrazyitems;


import com.madev.isellcrazyitems.handler.DatabaseHandler;
import com.madev.sales.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements
		OnClickListener {

	private Button btn_add, btn_view, btn_logout, btn_sync;

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_add:
			Intent addintent = new Intent(MainActivity.this,
					AddRecord.class);
			startActivity(addintent);
			break;

		case R.id.btn_view:
			Intent viewintent = new Intent(MainActivity.this,
					ViewRecord.class);
			startActivity(viewintent);
			break;
		case R.id.btn_sync:
			Intent syncintent = new Intent(MainActivity.this,
					SyncActivity.class);
			startActivity(syncintent);
			break;
		default:
			break;
		}

	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
        DatabaseHandler ldb = new DatabaseHandler(getApplicationContext());
        
        if(ldb.isUserLoggedIn())
        {
    		btn_add = (Button) findViewById(R.id.btn_add);
    		btn_view = (Button) findViewById(R.id.btn_view);
    		btn_logout = (Button) findViewById(R.id.btn_logout);
    		btn_sync = (Button) findViewById(R.id.btn_sync);
    		
    		btn_add.setOnClickListener(this);
    		btn_view.setOnClickListener(this);
    		btn_sync.setOnClickListener(this);
	
    		btn_logout = (Button) findViewById(R.id.btn_logout);
        	btn_logout.setOnClickListener(new View.OnClickListener() 
        	{
    			
    			@Override
				public void onClick(View arg0) {
    				// TODO Auto-generated method stub
    				DatabaseHandler ldb = new DatabaseHandler(getApplicationContext());
    				ldb.logoutUser();
    				Intent login = new Intent(getApplicationContext(), LoginActivity.class);
    	        	login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	        	startActivity(login);
    	        	// Closing dashboard screen
    	        	finish();
    			}
    		});
    	

        	
        }
        else
        {
        	// user is not logged in show login screen
        	Intent login = new Intent(getApplicationContext(), LoginActivity.class);
        	login.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        	startActivity(login);
        	// Closing dashboard screen
        	finish();
        }
		

	}
}